{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "67cd69c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "时间区间\t\t黄金涨幅\t上证指数涨幅\n",
      "2022-07-05 ~ 2022-10-31\t0.29%\t\t-15.00%\n",
      "2023-05-09 ~ 2024-02-05\t6.24%\t\t-19.52%\n",
      "2024-05-08 ~ 2024-09-18\t6.19%\t\t-13.14%\n",
      "2024-11-07 ~ 2024-11-26\t-0.08%\t\t-6.08%\n",
      "2024-12-10 ~ 2025-01-13\t2.78%\t\t-7.65%\n",
      "2025-03-31 ~ 2025-04-07\t-1.59%\t\t-7.17%\n",
      "\n",
      "累计涨幅对比（6 个区间）：\n",
      "黄金累计涨幅：13.83%\n",
      "上证累计涨幅：-68.56%\n"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "import pandas as pd\n",
    "\n",
    "# 时间区间列表\n",
    "time_ranges = [\n",
    "    (\"2022-07-05\", \"2022-10-31\"),\n",
    "    (\"2023-05-09\", \"2024-02-05\"),\n",
    "    (\"2024-05-08\", \"2024-09-18\"),\n",
    "    (\"2024-11-07\", \"2024-11-26\"),\n",
    "    (\"2024-12-10\", \"2025-01-13\"),\n",
    "    (\"2025-03-31\", \"2025-04-07\")\n",
    "]\n",
    "\n",
    "def get_au_close(start_date, end_date):\n",
    "    try:\n",
    "        df = ak.futures_main_sina(symbol=\"AU0\", start_date=start_date.replace(\"-\", \"\"), end_date=end_date.replace(\"-\", \"\"))\n",
    "        df[\"日期\"] = pd.to_datetime(df[\"日期\"])\n",
    "        df = df.sort_values(\"日期\").reset_index(drop=True)\n",
    "        df = df.rename(columns={\"收盘价\": \"close\"})\n",
    "        return df\n",
    "    except:\n",
    "        return None\n",
    "\n",
    "def get_sh_close(start_date, end_date):\n",
    "    try:\n",
    "        df = ak.stock_zh_index_daily(symbol=\"sh000001\")\n",
    "        df = df.copy()\n",
    "        df[\"date\"] = pd.to_datetime(df[\"date\"])\n",
    "        df = df[(df[\"date\"] >= pd.to_datetime(start_date)) & (df[\"date\"] <= pd.to_datetime(end_date))]\n",
    "        df = df.sort_values(\"date\").reset_index(drop=True)\n",
    "        return df\n",
    "    except:\n",
    "        return None\n",
    "\n",
    "print(\"时间区间\\t\\t黄金涨幅\\t上证指数涨幅\")\n",
    "\n",
    "total_gold = 0\n",
    "total_sh = 0\n",
    "valid = 0\n",
    "\n",
    "for start_date, end_date in time_ranges:\n",
    "    df_gold = get_au_close(start_date, end_date)\n",
    "    df_sh = get_sh_close(start_date, end_date)\n",
    "\n",
    "    if df_gold is None or len(df_gold) < 2 or df_sh is None or len(df_sh) < 2:\n",
    "        print(f\"{start_date} ~ {end_date}\\t数据不足\")\n",
    "        continue\n",
    "\n",
    "    gold_pct = (df_gold[\"close\"].iloc[-1] - df_gold[\"close\"].iloc[0]) / df_gold[\"close\"].iloc[0] * 100\n",
    "    sh_pct = (df_sh[\"close\"].iloc[-1] - df_sh[\"close\"].iloc[0]) / df_sh[\"close\"].iloc[0] * 100\n",
    "\n",
    "    print(f\"{start_date} ~ {end_date}\\t{gold_pct:.2f}%\\t\\t{sh_pct:.2f}%\")\n",
    "    \n",
    "    total_gold += gold_pct\n",
    "    total_sh += sh_pct\n",
    "    valid += 1\n",
    "\n",
    "if valid > 0:\n",
    "    print(f\"\\n累计涨幅对比（{valid} 个区间）：\")\n",
    "    print(f\"黄金累计涨幅：{total_gold:.2f}%\")\n",
    "    print(f\"上证累计涨幅：{total_sh:.2f}%\")\n",
    "else:\n",
    "    print(\"无有效区间数据\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
